#!/bin/bash
cd ~/app-root/data/temp/crx/abp/
domain="https://downloads.adblockplus.org/devbuilds/adblockpluschrome/"
build=$(curl -s ${domain} |grep -o 'adblockplus.*\.crx'|sed -e '2,$d' -e 's#.crx##')
if ! ls ~/app-root/repo/扩展/|grep -q "$build"; then
    curl ${domain}${build}.crx -o abp.zip
    unzip -o abp.zip _locales/en_US/* _locales/zh_CN/*
    unzip -o abp.zip -d do/
    rm -f abp.zip
    cd do/
    cp -f ~/app-root/data/github-sh/xpi/pan/do/chrome/content/ui/subscriptions.xml subscriptions.xml
    rm -rf _locales/
    mv ../_locales/ .
    sed -i -e '/#acceptableAdsContainer/,+4d' -e '/#acceptableAdsLink/d' -e '/#acceptableAdsDocs/,+5d' -e '/div id=\"acceptableAdsContainer/,/acceptableAdsLink/d' -e '/acceptableAdsDocs/,+1d' options.html
    sed -i -e '/defaults\.patternsbackups/s#5#0#' -e '/defaults\.privateBrowsing/s#false#true#' -e '/defaults\.suppress_first_run_page/s#false#true#' lib/adblockplus.js
    sed -i -e '/defaults\.subscriptions_fall/d' -e '/defaults\.subscriptions_exception/d' -e '/defaults\.subscriptions_antiadblock/d' lib/adblockplus.js
    sed -i -e '/var acceptableAdsUrl/d' -e "/subscriptions_exceptionsurl/, $(($(sed -n '/#acceptableAdsLink/=' options.js)+1))d" -e "/acceptable_ads/, $(($(sed -n '/#acceptableAdsDocs/=' options.js)+1))d" options.js
    sed -i -e '/#acceptableAds\").click/d' -e "$(sed -n '/\ \{3,\}if (subscription.url == acceptableAdsUrl)/=' options.js|sed '2,$d'),+2d" -e '/function toggleAcceptableAds/,+4d' options.js
    sed -i -e '/if (subscription.url == acceptableAdsUrl)/,+1d' -e "$(($(sed -n '/element.parentNode.removeChild(element);/=' options.js)-1))s#else ##" options.js
    sed -i '/minimum_opera_version/d' manifest.json
    rm -f firstRun.*
    zip -r abp.zip ./
    rm -f ~/app-root/repo/扩展/adblockpluschrome*
    mv -f abp.zip ~/app-root/repo/扩展/$build.zip
    rm -rf * .*
fi